package com.dq.domain.subscribe;

import com.dq.domain.base.BaseEntity;

import com.dq.domain.service.ServiceProject;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.vo.baseVo.VO;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/20 0020 10:24:11
 * @description 预约实体
 */
@Data
@Entity
@ApiModel("预约实体")
public class Subscribe extends BaseEntity implements VO.ToVO{
    @ApiModelProperty("预约编号")
    @NotNull
    protected String subscribeNo;
    @ApiModelProperty("预约顾客")
    @ManyToOne
    protected WxUser wxUser;
    @ApiModelProperty("预约技师")
    @ManyToOne
    protected BossUser technicianUser;

    @ApiModelProperty("预约门店")
    @ManyToOne
    protected Org sysDept;

    @ApiModelProperty
    @ManyToOne
    protected SysSection originalOrg;

    @ApiModelProperty("预约开始时间")
    protected Long beginTime;
    @ApiModelProperty("预约结束时间")
    protected Long endTime;

    @ApiModelProperty("预约状态")
    @Enumerated(EnumType.ORDINAL)
    protected SubscribeStatus subscribeStatus;
    @ApiModelProperty("是否支付")
    protected Boolean isPay = false;
    @ApiModelProperty("支付时间")
    private Long payTime;

    @ApiModelProperty("反馈")
    protected String feedBack;

    @ApiModelProperty("服务返金")
    protected BigDecimal serviceBackAmount;

    @ApiModelProperty("项目名")
    protected String projectName;

    @ApiModelProperty("项目类型")
    protected String projectCategoryName;

    @ApiModelProperty("服务手工费")
    protected BigDecimal manualPrice;

    @ApiModelProperty("超时时间")
    protected Long timeoutTime;

    @ApiModelProperty("服务项目")
    @ManyToOne
    protected ServiceProject serviceProject;

    @ApiModelProperty("签字图片")
    protected String signatureImg;

    @ApiModelProperty("订单来源(线下订单,线上订单,积分订单,活动订单)")
    @Enumerated(EnumType.ORDINAL)
    protected SubscribeSource subscribeSource;

    @ApiModelProperty("支付方式")
    @Enumerated(EnumType.ORDINAL)
    protected SubscribePayType subscribePayType;


    @ApiModelProperty("总支付积分")
    protected Long paySumIntegral;
    @ApiModelProperty("总支付金额")
    protected BigDecimal price;

    @ApiModelProperty(value = "应付金额")
    private BigDecimal originalPrice;

    @ApiModelProperty("预约类型")
    @Enumerated(EnumType.ORDINAL)
    private SubscribeType subscribeType;

    @ApiModelProperty("到店时间")
    private Long arrivalTime;

    @ApiModelProperty("sign")
    @Column(columnDefinition = "text")
    private String sign;
    @ApiModelProperty("qrOssUrl")
    private String qrOssUrl;


    public enum SubscribeStatus{
        /**
         * 预约延时通知判断状态
         */
        confirmed("预约成功"),
        processing("待确认"),
        refuse("已拒绝"),
        cancel("已失效"),
        cancelled("已核销"),
        inservice("服务中"),
        stayPaid("待结账"),
        paid("已结账"),
        signature("已签字"),
        serviced("服务结束")
        ;
        public String name;

        SubscribeStatus(String name){
            this.name = name;
        }
    }

    public enum SubscribeSource{
        onLine("线上"),underLine("线下"), integral("积分"), activity("活动");
        public String name;

        SubscribeSource(String name){
            this.name = name;
        }
    }

    public enum SubscribePayType {
        alipay("支付宝"),
        wxpay("微信"),
        card("刷卡支付"),
        cash("现金支付"),
        online("在线支付"),
        vipCard("会员卡积分"),
        vipCardProject("会员卡项目次数结账")
        ;

        public String name;

        SubscribePayType(String name) {
            this.name = name;
        }
    }

    public enum SubscribeType{
        cross("跨店预约"),
        normal("正常预约")
        ;
        public String name;

        SubscribeType(String name) {
            this.name = name;
        }
    }

    @Data
    @ApiModel(value = "预约视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty("预约编号")
        protected String subscribeNo;
        protected Long wxUserId;
        @ApiModelProperty("用户真实姓名")
        protected String wxUserRealcName;
        protected Long orgId;
        protected String orgName;
        protected String orgContaceMobile;
        protected Long staffUserId;
        protected String staffUserRealcName;
        @ApiModelProperty("预约开始时间")
        protected String beginTime;
        @ApiModelProperty("预约结束时间")
        protected String endTime;
        @ApiModelProperty("预约状态")
        protected String subscribeStatusCN;
        @ApiModelProperty("预约状态")
        protected String subscribeStatusEN;

        @ApiModelProperty("sign")
        private String sign;
        @ApiModelProperty("qrOssUrl")
        private String qrOssUrl;
        @ApiModelProperty("是否支付")
        protected Boolean isPay;

        @ApiModelProperty("反馈")
        protected String feedBack;
        @ApiModelProperty("服务返金")
        protected BigDecimal serviceBackAmount;
        @ApiModelProperty("服务手工费")
        protected BigDecimal manualPrice;
        protected Long serviceProjectId;
        @ApiModelProperty("项目名")
        protected String projectName;
        @ApiModelProperty("签字图片")
        protected String signatureImg;
        @ApiModelProperty("标签(数目)")
        protected List<String> lableStrList;
        protected String subscribeTypeEn;
        protected String subscribeTypeCn;
        protected String arrivalTime;
        protected String mobile;
        protected String cardNo;
        protected Long projectTime;
        protected BigDecimal projectPrice;
        protected Long flag;
        protected String subscribeType;
        @ApiModelProperty("评价记录id")
        protected Long evaluationRecordId;
        @ApiModelProperty("消耗提成比例")
        protected BigDecimal expendRatio;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.getId();
        vo.subscribeNo = this.getSubscribeNo();
        vo.wxUserId = this.getWxUser() != null ? this.getWxUser().getId() : null;
        vo.wxUserRealcName = this.getWxUser() != null ? this.getWxUser().getUserRealcName() : "";
        vo.orgId = this.getSysDept() != null ? this.getSysDept().getId() : null;
        vo.orgName = this.getSysDept() != null ? this.getSysDept().getSimpleName() : "";
        vo.orgContaceMobile = this.getSysDept() != null ? this.getSysDept().getContaceMobile() : "";
        vo.staffUserId = this.getTechnicianUser() != null ? this.getTechnicianUser().getId() : null;
        vo.staffUserRealcName = this.getTechnicianUser() != null ? this.getTechnicianUser().getUserRealcName() : "";
        vo.beginTime = fmt(this.beginTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.endTime = fmt(this.endTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.arrivalTime = fmt(this.arrivalTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.subscribeStatusCN = this.getSubscribeStatus().name;
        vo.subscribeStatusEN = this.getSubscribeStatus().toString();
        vo.subscribeTypeCn = this.getSubscribeType().name;
        vo.subscribeTypeEn = this.getSubscribeType().toString();
        vo.feedBack = this.getFeedBack();
        vo.serviceBackAmount = this.getServiceBackAmount();
        vo.manualPrice = this.getManualPrice();
        vo.serviceProjectId = this.getServiceProject() != null ? this.getServiceProject().getId():null;
        vo.projectName = this.getServiceProject() != null ? this.getServiceProject().getProjectName() : "";
        vo.signatureImg = this.getSignatureImg();
        vo.mobile = this.getWxUser() != null ? this.getWxUser().getMobile() : "";
        vo.projectTime = this.getServiceProject() != null ? this.getServiceProject().getServiceTime() : null;
        vo.projectPrice = this.getServiceProject() != null ? this.getServiceProject().getProjectPrice() : BigDecimal.ZERO;
        vo.subscribeType = this.getSubscribeType() != null ? this.getSubscribeType().toString():"";

        vo.sign = fmt(this.getSign());
        vo.qrOssUrl = fmt(this.getQrOssUrl());
        vo.isPay = this.getIsPay();
        vo.expendRatio = this.getTechnicianUser() != null && this.getTechnicianUser().getExpendRatio()!=null  ? this.getTechnicianUser().getExpendRatio():BigDecimal.ZERO;
        return vo;
    }

    @Data
    @ApiModel("我的预约单列表")
    public static class SubscribeListVo{
        @ApiModelProperty("预约单id")
        protected Long id;
        @ApiModelProperty("门店名称")
        protected String orgName;
        @ApiModelProperty("技师名字")
        protected String staffUserName;
        @ApiModelProperty("预约开始时间")
        protected String beginTime;
        @ApiModelProperty("预约结束时间")
        protected String endTime;
        @ApiModelProperty("预约状态")
        protected String subscribeStatus;
        @ApiModelProperty("预约状态")
        protected String subscribeStatusEN;
        @ApiModelProperty("总条数")
        protected Long totalNum;
    }

    @Data
    @ApiModel("门店顾客预约记录")
    public static class OrgSubscribeListVo{
        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("预约开始时间")
        protected String beginTime;
        @ApiModelProperty("预约结束时间")
        protected String endTime;
        @ApiModelProperty("预约状态")
        protected String status;
        protected String statusEn;
        @ApiModelProperty("门店")
        protected Long orgId;
        protected String orgName;

        @ApiModelProperty("预约技师姓名")
        protected String getUserRealcName;
        @ApiModelProperty("会员等级")
        protected String wxUserLevel;
        @ApiModelProperty("会员卡号")
        protected String cardNo;
        @ApiModelProperty("会员姓名")
        protected String wxUserRealcName;
    }
    public OrgSubscribeListVo toOrgSubscribeListVo(){
        Subscribe.OrgSubscribeListVo orgSubscribeListVo = new Subscribe.OrgSubscribeListVo();
        orgSubscribeListVo.id = this.getId();
        orgSubscribeListVo.beginTime = fmt(this.getBeginTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgSubscribeListVo.endTime = fmt(this.getEndTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgSubscribeListVo.status = fmt(this.getSubscribeStatus().name);
        orgSubscribeListVo.statusEn = fmt(this.getSubscribeStatus().toString());

        orgSubscribeListVo.orgId = this.sysDept == null ? null : this.sysDept.getId();
        orgSubscribeListVo.orgName = this.sysDept == null ? "" : this.sysDept.getSimpleName();

        BossUser staffUser = this.getTechnicianUser();
        orgSubscribeListVo.getUserRealcName =staffUser!=null? fmt(staffUser.getUserRealcName()):"";
        WxUser wxUser = this.getWxUser();
        orgSubscribeListVo.wxUserLevel =wxUser!=null && wxUser.getOrgCustomerLevel() !=null ? fmt(wxUser.getOrgCustomerLevel().getLevenName()):"";
        orgSubscribeListVo.cardNo = fmt(wxUser.getCardNo());
        orgSubscribeListVo.wxUserRealcName = fmt(wxUser.getUserRealcName());
        return orgSubscribeListVo;
    }


    public SubscribeListVo toSubscribeListVo(){
        SubscribeListVo vo = new SubscribeListVo();
        vo.id = this.id;
        vo.orgName = this.sysDept.getSimpleName();
        vo.staffUserName = this.technicianUser.getUserRealcName();
        vo.beginTime = fmt(this.beginTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.endTime = fmt(this.endTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.subscribeStatus = this.subscribeStatus.name;
        vo.subscribeStatusEN = this.subscribeStatus.toString();
        return vo;
    }

    @Data
    @ApiModel("技师看自己待处理预约记录")
    public static class StaffUserSubscribe{
        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("预约开始时间")
        protected String beginTime;
        @ApiModelProperty("预约结束时间")
        protected String endTime;
        @ApiModelProperty("顾客姓名")
        protected String wxUserName;
        @ApiModelProperty("创建时间")
        protected String createTime;
        @ApiModelProperty("会员等级")
        protected String wxUserLevel;
        @ApiModelProperty("会员卡号")
        protected String cardNo;
        @ApiModelProperty("预约状态")
        protected String subscribeStatus;
        @ApiModelProperty("预约状态en")
        protected SubscribeStatus subscribeStatusEn;
        @ApiModelProperty("顾客用户id")
        protected Long wxUserId;

    }

    public StaffUserSubscribe toStaffUserSubscribe(){
        StaffUserSubscribe vo = new StaffUserSubscribe();
        vo.id = this.getId();
        vo.beginTime = fmt(this.beginTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.endTime = fmt(this.endTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.createTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        WxUser wxUser = (WxUser)this.wxUser;
        vo.wxUserName = wxUser.getUserRealcName();
        vo.wxUserLevel = wxUser.getOrgCustomerLevel()!= null ? wxUser.getOrgCustomerLevel().getLevenName():"";
        vo.cardNo = fmt(wxUser.getCardNo());
        vo.subscribeStatus = this.subscribeStatus.name;
        vo.subscribeStatusEn = this.subscribeStatus;
        vo.wxUserId = this.wxUser.getId();
        return vo;
    }


    @Data
    @ApiModel("技师看自己待处理预约记录")
    public static class ListSubscribe{
        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("预约开始时间")
        protected String beginTime;
        @ApiModelProperty("预约结束时间")
        protected String endTime;
        @ApiModelProperty("顾客姓名")
        protected String wxUserName;
        @ApiModelProperty("创建时间")
        protected String createTime;
        @ApiModelProperty("会员等级")
        protected String wxUserLevel;
        @ApiModelProperty("会员卡号")
        protected String cardNo;
        @ApiModelProperty("预约状态")
        protected String subscribeStatus;
        @ApiModelProperty("预约状态en")
        protected SubscribeStatus subscribeStatusEn;
        @ApiModelProperty("顾客用户id")
        protected Long wxUserId;
        protected Long serviceRecordId;
    }

    public ListSubscribe toListSubscribe(Long serviceRecordId){
        ListSubscribe vo = new ListSubscribe();
        vo.id = this.getId();
        vo.beginTime = fmt(this.beginTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.endTime = fmt(this.endTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.createTime,TimeUtil.YYYY_MM_DD_HH_MM_SS);
        WxUser wxUser = (WxUser)this.wxUser;
        vo.wxUserName = wxUser.getUserRealcName();
        vo.wxUserLevel = wxUser.getOrgCustomerLevel()!= null ? wxUser.getOrgCustomerLevel().getLevenName():"";
        vo.cardNo = fmt(wxUser.getCardNo());
        vo.subscribeStatus = this.subscribeStatus.name;
        vo.subscribeStatusEn = this.subscribeStatus;
        vo.wxUserId = this.wxUser.getId();
        vo.serviceRecordId = serviceRecordId;
        return vo;
    }
}
